define(["angularAMD", "moment", "module/system/program/myFavourite/favouriteService"], function (moment) {
    return ['$rootScope', "$scope", "$filter", "$stateParams", "config", "MMWService", "RouterExtendProvider", "$mdDialog", "favouriteService",
    function ($rootScope, $scope, $filter, $stateParams, config, $MMWService, $RouterExtendProvider, $mdDialog, $favouriteService) {
        // 20220726 modify by Alan for#107965 : 配合規格進行調整
        $scope.favouriteInfo = {
            UserNo: '',
            FavouriteList: [],
            DefaultProgram : ''
        };

        $scope.init = function(){
            $favouriteService.get(config.cache.account, (data) => {
                $scope.favouriteInfo = data;
            });
        }

        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        }

        $scope.confirm = function(){
            //确认按钮
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        }

        //sMES 2.1.0 以后新增
        $scope.final = function(callback){
            //当离开此程序的时候一定会执行此函式
            callback();//结束程序
        }
        
        $scope.editFavourite = function() {
            $RouterExtendProvider.myFavourite('edit');
        }

        $scope.hasFavourite = function() {
            return $scope.favouriteInfo.FavouriteList.length > 0;
        }

        $scope.isFavourite = function(program) {
            return $scope.favouriteInfo.FavouriteList.includes(program.name) > 0 && program.isShow
        }
        
        // 20230310 modify by Alan for#129828 : 我的最愛報錯
        $scope.isModuleHaveFavourite = function(module) {
            return module.childs && module.childs.some(program => {
                return $scope.isFavourite(program);
            });
        }
        
        $scope.openOptionDialog = function(program, event){
            event.stopPropagation();
            $mdDialog.serviceDialog('optionDialogService', {
                item: program
            }, (dialog) => $scope.favouriteInfo = dialog.favouriteInfo);
        }
        
        $scope.executeProgram = function(program) {
            $RouterExtendProvider.goToProgram(program);
        }
    }
]});
